package com.common.boot.util;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class HttpUtil {

    public static String sendGet(String url) throws IOException {
        // 创建默认的客户端实例
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建get请求实例
        HttpGet httpGet = new HttpGet(url);
        // 设置头部信息
//        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
        httpGet.setHeader("Cookie", "SECKEY_ABVK=CcVfxkxzrTM7MOFTuoMa4KiAd3r/EsvHkZkcNhYGNxY%3D; BMAP_SECKEY=lz83JxI4Xo_L9fclAIc1tszRls2aEFGy6_r2giYuhPYiZG16iB0o4XELDFUFp2X_hc3iHCgR5epIPm_Or2zScj6dxvVmL5epnBQlOFcmBne06kK1255nLeV3Vz02oww_7c9gdF8OX1mb7g7Q4REdgIMNfa6bHYWvrUpbwpEWsuwU6BRrF0HqQz4fIGDvgPgr; userName=ytjAdmin; SESSION=ZjNjZjczY2QtYTYyOC00N2ExLTkzZGMtZjMyMmU0MDRmZjIw; JSESSIONID=7ndnhywwaags1019u1jn5ex6p");
        // 执行GET请求 返回响应实体
        HttpResponse response = httpclient.execute(httpGet);
        // 获取响应码
        int statusCode = response.getStatusLine().getStatusCode();
        String s = "";
        if (statusCode == 200) {
            // 获取响应消息实体
            HttpEntity entity = response.getEntity();
            s = EntityUtils.toString(entity);
        }
        // 关闭连接
        try {
            httpclient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return s;
    }

    @SneakyThrows
    public static String sendPost(Map param, String url) {
        // 创建默认的客户端实例
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建POST对象
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-type", "application/json");
//        httpPost.setHeader("Cookie","JSESSIONID=77AF4B43867ECE4468E03BB744DD2312; SECKEY_ABVK=nVbXf+nJ8/zEqLkh2vEJrSAmL+HFCHGrGXPgcwxqgh8%3D; BMAP_SECKEY=lz83JxI4Xo_L9fclAIc1tgDAdEXvKb9Ck3LCJ8vKQqPY1_ABfjXZOgt1EfJ8hY2fgKjT_3RY5-7jzQTdoQZHwuYkZCIjktxleHcDBWrhyawX4cVAQjtc1Dr9UkRRllU4NmVdSR8oM6zaxnqzqtOhJwTyeYNlHK3q_FU-2LgznEEgM_1BdAwm3x0HUrCEDHUz; rmbUser=remembered; userName=hqAdmin; passWord=wwhl123456; SESSION=ODA4MzRhMTktNDViMy00MDE4LWJjYjktNjcxOWI3MTMyMDlk");
        // 传参数
        StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(param), "UTF-8");
        httpPost.setEntity(stringEntity);
        // 执行GET请求 返回响应实体
        HttpResponse response = httpclient.execute(httpPost);
        // 获取响应码
        int statusCode = response.getStatusLine().getStatusCode();
        // 获取响应消息实体
        HttpEntity entity = response.getEntity();
        String s = EntityUtils.toString(entity);
        // 关闭连接
        try {
            httpclient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return s;
    }
}